package com.tank.components;

import com.almasb.fxgl.core.math.FXGLMath;
import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.EffectComponent;
import com.almasb.fxgl.dsl.components.HealthIntComponent;
import com.almasb.fxgl.entity.component.Component;
import com.tank.constant.Config;
import com.tank.constant.Direction;
import com.tank.effect.FreezeEffect;

public class EnemyComponent extends Component {

    private TankComponent tankComponent;

    private int type;

    public EnemyComponent(int enemyType) {
        type = enemyType;
    }

    @Override
    public void onAdded() {
        HealthIntComponent hpComponent = entity.getComponent(HealthIntComponent.class);
        if (type <= 6) {
            hpComponent.setValue(1);
        } else if (type <= 9) {
            hpComponent.setValue(2);
        } else {
            hpComponent.setValue(3);
        }
    }

    @Override
    public void onUpdate(double tpf) {
        if (entity.getComponent(EffectComponent.class).hasEffect(FreezeEffect.class)) {
            return;
        }
        Direction direction = tankComponent.getDirection();
        if (FXGLMath.randomBoolean(0.025)) {
            direction = FXGLMath.random(Direction.values()).get();
        }
        tankComponent.moveForDirection(direction);
        if (FXGLMath.randomBoolean(0.08)) {
            tankComponent.shoot();
        }
    }

    @Override
    public void onRemoved() {
        FXGL.inc(Config.ENEMY_DESTROY_AMOUNT, 1);
        FXGL.spawn("item", FXGLMath.random(1, (Config.GROUND_CELL_COUNT - 1) * Config.CELL_SIZE), FXGLMath.random(1, (Config.GROUND_CELL_COUNT - 1) * Config.CELL_SIZE));
    }
}
